package com.xja.service.impl;

import com.xja.connutil.ConnUtil;
import com.xja.dao.ZcyDao;
import com.xja.dao.impl.ZcyDaoimpl;
import com.xja.entity.Indent;
import com.xja.entity.Room;
import com.xja.entity.pojo.BillsAndRoom;
import com.xja.entity.pojo.UrByIndent;
import com.xja.service.ZcyService;

import java.sql.SQLException;
import java.util.HashMap;
import java.util.List;

/**
 * @author zcy
 * @create 2022-05-05 20:00
 */
public class ZcyServiceImpl implements ZcyService {
    ZcyDao dao = new ZcyDaoimpl();

    @Override
    public List<Room> findRoom() {
        List<Room> list=null;
        try {
            list = dao.findRoom();
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }finally {
            ConnUtil.closeConn();
        }
        return list;
    }

    @Override
    public boolean addRoom(Room room) {
        Integer b =null;
        try {
            b = dao.addRoom(room);
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }finally {
            ConnUtil.closeConn();
        }
        if (b==null||b==0){
            return false;
        }else {
            return true;
        }
    }

    @Override
    public Room findRoomById(int updateId) {
        Room room=null;
        try {
            room = dao.findRoomById(updateId);
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }finally {
            ConnUtil.closeConn();
        }
        return room;
    }

    @Override
    public boolean updateRoom(Room updateroom) {
        Integer i=null;
        try {
            i = dao.updateRoom(updateroom);
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }finally {
            ConnUtil.closeConn();
        }
        if (i==null||i==0){
            return false;
        }else {
            return true;
        }

    }

    @Override
    public boolean deleteRoomById(int deleteId) {
        Integer i=null;
        try {
            i =dao.deleteRoomById(deleteId);
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }finally {
            ConnUtil.closeConn();
        }
        if (i==null||i==0){
            return false;
        }else {
            return true;
        }

    }

    @Override
    public List<UrByIndent> FindAllIndent() {
        List<UrByIndent> list=null;
        try {
            list = dao.FindAllIndent();
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }finally {
            ConnUtil.closeConn();
        }
        return list;
    }

    @Override
    public boolean deleteIndentById(int deleteIndentById) {
        Integer i=null;
        try {
            i =dao.deleteIndentById(deleteIndentById);
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }finally {
            ConnUtil.closeConn();
        }
        if (i==0||i==null){
            return false;
        }else {
            return true;
        }

    }

    @Override
    public boolean updateIndent(Indent updateIndent) {
        Integer i=null;
        try {
            i =dao.updateIndent(updateIndent);
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }finally {
            ConnUtil.closeConn();
        }if (i==0||i==null){
            return false;
        }else {
            return true;
        }
    }

    @Override
    public boolean updateStatusById(Integer roomId, Integer status) {
        Integer i=null;
        try {
            i =dao.updateStatusById(roomId,status);
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return false;
    }

    @Override
    public HashMap<String, String> findBroom() {
        List<BillsAndRoom> broom=null;
        try {
            broom = dao.findBroom();
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }finally {
            ConnUtil.closeConn();
        }
        HashMap<String, String> map = new HashMap<>();
        if (broom.size()!=0){
            for (BillsAndRoom billsAndRoom : broom) {
                boolean f = false;
                for (String s : map.keySet()) {
                    if (billsAndRoom.getRoom().getRoomKind().equals(s)){
                        String s1 = map.get(s);
                        String selectSum = billsAndRoom.getBills().getSelectSum();
                        int s11 = Integer.parseInt(s1);
                        Integer selectSum1 = Integer.parseInt(selectSum);
                        Integer sumMoney=s11+selectSum1;
                        String StrSumMoney = String.valueOf(sumMoney);
                        map.put(s,StrSumMoney);
                        f=true;
                        break;
                    }
                }
                if (f==false){
                    map.put(billsAndRoom.getRoom().getRoomKind(),billsAndRoom.getBills().getSelectSum());
                }

            }

        }

        return map;
    }
}
